<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>推荐位管理</el-breadcrumb-item>
        <el-breadcrumb-item>书城-精选 </el-breadcrumb-item>
        <el-breadcrumb-item>人气手写</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel"
      >
        <template slot="last" slot-scope="scope">
          {{ scope.row.edit_time }}
          <el-tag>{{ scope.row.edit_account_name }}</el-tag>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { catRecommend as baseURL, catRecommendSort } from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10,
        // pageSizes:[1,2,3]
      },
      type: true,
      data: [],
      params: {},
      form: {},
      default: {
        orderByList: "sort&ASC,create_time&ASC",
        style:2
      },
      option: {
        index: true,
        indexLabel: "编号",  
        tip: true,
        addBtn: true,
        editBtn: true,
        excelBtn: false,
        refreshBtn: true,
        searchBtn: false,
        saveBtn: true,
        viewBtn: false,
        delBtn: true,
        column: [
          {
            label: "推荐位",
            prop: "style",
            type: "select",
            search: false,
            value:2,
            // dicUrl: `${catUrl + catRecommendSort.getList}`,
            dicData:[{title:'人气写手',style:2}],
            rules: [
              {
                required: true,
                message: "请选择推荐位",
              },
            ],
            props: {
              label: "title",
              value: "style",
            },
            editDisabled: true,
            addDisabled: true,
            value:2,
          },
          {
            label: "作者id",
            prop: "user_id",
            type: "input", 
            search: false,
          },
          {
            label: "代表作id",
            prop: "book_id",
            type: "input",  
            search: false,
          },
          {
            label: "排序",
            prop: "sort",
            type: "number",
            search: false,
          },
          {
            label: "最后操作信息",
            prop: "last",
            type: "input",
            addDisplay: false,
            editDisplay: false,
            addDisabled: false,
            editDisabled: false,
            readonly: true,
            slot: true,
          },
        ],
      },
    };
  },
  methods: {
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseURL.pageAndParam,
        { ...this.params, ...this.default },
        this.page,
        "POST"
      );
      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    },
    /**保存 */
    async rowSave(row, done, loading) {
      delete row["id"];
      row.create_time = new Date();
      let userInfo = this.$store.state.user.userInfo;
      row.uid = userInfo.id?userInfo.id:null;
      row.edit_account_name = userInfo.username;
      let data = await common(baseURL.save, row, null, "POST");
      if (data == null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      let userInfo = this.$store.state.user.userInfo;
      row.uid = userInfo.id?userInfo.id:null;
      row.edit_account_name = userInfo.username;
      row.edit_time = new Date();
      let data = await common(baseURL.update, row, null, "POST");
      if (data == null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**删除 */
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          let response = await common(
            baseURL.delete,
            { id: row.id },
            null,
            "GET"
          );
          // if (response == null) {
          //   return this.$message.error("操作失败");
          // }
        })
        .then(() => {
          this.getList();
        });
    },
    /**搜索 */
    searchChange(params, done) {
      
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  },
};
</script>